package com.xuce.nzshopemailserver.controller;

import com.xuce.constant.RedisConstant;
import com.xuce.dto.ResultBean;
import com.xuce.nzshopemailserver.service.IEmailService;
import com.xuce.pojo.TUser;
import com.xuce.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author 徐策
 * @date 2020/3/10 20:04
 */
@RestController
@RequestMapping("email")
public class EmailController {
    //JavaMailSender
    @Autowired
    private JavaMailSender sender;

    //模板引擎
    @Autowired
    private TemplateEngine templateEngine;

    @Value("${activeServerUrl}")
    private String activeServerUrl;

    //redis
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    IEmailService emailService;
    /*
    //http://localhost:8763/email/active/account/339934c6-31db-4e8c-a43b-d604f6111cb1
    激活链接
     */
    @RequestMapping("active/account/{uuid}")
    public ResultBean activateAccount(@PathVariable String uuid){
        //1.组织redis中的键
        String redisKey = RedisUtil.getRedisKey(RedisConstant.REGIST_EMAIL_URL_KEY_PRE, uuid);

        //2.得到邮箱账号
        String addr = (String) redisTemplate.opsForValue().get(redisKey);

        TUser tUser = new TUser();
        tUser.setEmail(addr);
//        TODO 更新数据库 用户状态从0改成1
        //3.激活账号
        int result = emailService.activeByEmail(tUser);
        if(result!=0){
            return ResultBean.success("激活成功");
        }
        return ResultBean.error("激活失败");
//        return ResultBean.success("激活成功");
    }

    //发送激活邮件
    //http://localhost:8763/email/send/2528193681@qq.com/339934c6-31db-4e8c-a43b-d604f6111cb1
    @RequestMapping("send/{addr}/{uuid}")
    public ResultBean sendEmail(@PathVariable String addr,
                                @PathVariable String uuid) {

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper mailMessage = null;

        try {
            mailMessage = new MimeMessageHelper(message,true);
            mailMessage.setSubject("请激活账号");

            Context context = new Context();
            context.setVariable("username",addr.substring(0,addr.lastIndexOf('@')));
            //http://localhost:8763/email/active/account/{uuid}
            context.setVariable("url",activeServerUrl+uuid);
            String info = templateEngine.process("emailTemplate", context);
            mailMessage.setText(info,true);

            mailMessage.setFrom("1256075539@qq.com");//系统账号
            mailMessage.setTo(addr);
            System.out.println("uuid："+uuid);
            sender.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return  ResultBean.success("email send success");
    }
}
